package com.ruoyi.business.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.constant.CarConstant;
import com.ruoyi.business.domain.BusCar;
import com.ruoyi.business.domain.BusRent;
import com.ruoyi.business.dto.CarDto;
import com.ruoyi.business.mapper.BusAttachmentMapper;
import com.ruoyi.business.mapper.BusCarMapper;
import com.ruoyi.business.mapper.BusRentMapper;
import com.ruoyi.business.vo.CustomerAddressVo;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BusCustomerMapper;
import com.ruoyi.business.domain.BusCustomer;
import com.ruoyi.business.service.IBusCustomerService;

/**
 * 客户增删改查Service业务层处理
 *
 * @author powernode
 * @date 2021-12-28
 */
@Service
@Slf4j
public class BusCustomerServiceImpl implements IBusCustomerService {
    @Autowired
    private BusCustomerMapper busCustomerMapper;
    @Autowired
    private ISysDeptService iSysDeptService;
    @Autowired
    private BusRentMapper busRentMapper;


    /**
     * 查询客户增删改查
     *
     * @param id 客户增删改查主键
     * @return 客户增删改查
     */
    @Override
    public BusCustomer selectBusCustomerById(Long id) {
        return busCustomerMapper.selectBusCustomerById(id);
    }

    /**
     * 查询客户增删改查列表
     *
     * @param busCustomer 客户增删改查
     * @return 客户增删改查
     */
    @Override
    public List<BusCustomer> selectBusCustomerList(BusCustomer busCustomer) {
        return busCustomerMapper.selectBusCustomerList(busCustomer);
    }

    /**
     * 新增客户增删改查
     *
     * @param busCustomer 客户增删改查
     * @return 结果
     */
    @Override
    public int insertBusCustomer(BusCustomer busCustomer) {
        busCustomer.setCreateTime(DateUtils.getNowDate());
        return busCustomerMapper.insertBusCustomer(busCustomer);
    }

    /**
     * 修改客户增删改查
     *
     * @param busCustomer 客户增删改查
     * @return 结果
     */
    @Override
    public int updateBusCustomer(BusCustomer busCustomer) {
        busCustomer.setUpdateTime(DateUtils.getNowDate());
        return busCustomerMapper.updateBusCustomer(busCustomer);
    }

    /**
     * 批量删除客户增删改查
     *
     * @param ids 需要删除的客户增删改查主键
     * @return 结果
     */
    @Override
    public int deleteBusCustomerByIds(Long[] ids) {
        return busCustomerMapper.deleteBusCustomerByIds(ids);
    }

    /**
     * 删除客户增删改查信息
     *
     * @param id 客户增删改查主键
     * @return 结果
     */
    @Override
    public int deleteBusCustomerById(Long id) {
        return busCustomerMapper.deleteBusCustomerById(id);
    }

    /**
     * 分页查询客户的列表
     *
     * @param loginUser
     * @param busCustomer
     * @return
     */
    @Override
    public List<BusCustomer> selectCustomerPage(LoginUser loginUser, BusCustomer busCustomer) {
        return busCustomerMapper.selectCustomerPage(loginUser, busCustomer);
    }

    /**
     * 新增客户
     * @param busCustomer
     * @param loginUser
     * @return
     */
    @Override
    public int addCustomer(BusCustomer busCustomer, LoginUser loginUser) {
        log.info("新增客户{}，当前用户id为{}", JSON.toJSONString(busCustomer), loginUser.getUserId());
        // 设置默认值
        busCustomer.setCreateTime(new Date());
        busCustomer.setUpdateTime(new Date());
        busCustomer.setCreateBy(loginUser.getUsername());
        busCustomer.setDelFlag(CarConstant.DEL_FLAG_UNDEL);
        // 处理部门的名称
        // 拿到部门的id  查询部门的名称
        SysDept sysDept = iSysDeptService.selectDeptById(busCustomer.getDeptId());
        busCustomer.setDeptName(sysDept.getDeptName());
        return busCustomerMapper.insertBusCustomer(busCustomer);
    }

    /**
     * 修改客户
     * @param busCustomer
     * @param loginUser
     * @return
     */
    @Override
    public int updateCustomer(BusCustomer busCustomer, LoginUser loginUser) {
        log.info("修改客户{}，当前用户id为{}", JSON.toJSONString(busCustomer), loginUser.getUserId());
        busCustomer.setUpdateTime(new Date());
        busCustomer.setUpdateBy(loginUser.getUsername());
        //修改也是传入部门的id进来
        SysDept sysDept = iSysDeptService.selectDeptById(busCustomer.getDeptId());
        busCustomer.setDeptName(sysDept.getDeptName());
        return busCustomerMapper.updateBusCustomer(busCustomer);
    }

    /**
     * 删除客户
     * @param ids
     * @param loginUser
     * @return
     */
    @Override
    public int delCustomer(List<Long> ids, LoginUser loginUser) {
        log.info("删除客户{}，当前用户的id为{}",JSON.toJSONString(ids),loginUser.getUserId());
        // 我们不能在循环中操作数据库  能发一条sql就发一条  foreach  update customer set del_flag = 1 , update = xxx where id in (1,2)
        return busCustomerMapper.delCustomerByIds(ids,loginUser);
    }

    /**
     * 根据客户的身份号校验客户信息
     * 1.查询客户表 看是否存在
     * 2.该客户有没有为归还的车辆
     *
     * @param identity
     * @return
     */
    @Override
    public Boolean checkCustomerId(String identity) {
        BusCustomer busCustomer = busCustomerMapper.selectByIdentity(identity);
        if (ObjectUtils.isEmpty(busCustomer)){
            throw new IllegalArgumentException("客户身份不存在,请先登记客户身份再租车");

        }
        //该客户有没有归还的车辆
        BusRent busRent = busRentMapper.selectRentByIdentityAndRentStatus(identity,CarConstant.CAR_NO_BACK);
        if (!ObjectUtils.isEmpty(busRent)){
            throw new IllegalArgumentException("该客户有车辆未归还,请先归还后再租车");

        }
        return true;
    }

    /**
     * 查询客户地址数据
     * @return
     */
    @Override
    public List<CustomerAddressVo> getCustomerAddressData() {
        return busCustomerMapper.selectCustomerAddressData();
    }

}
